//
//  QuartzLine.m
//  QuartzStudy
//
//  Created by longhuihu on 11-6-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LineShape.h"
#import "BaseShape.h"

@interface SimpleLineView : BaseShape {
}
@end
@implementation SimpleLineView
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetStrokeColor(context, colorComponents);
	CGContextMoveToPoint(context, 5, 5);
	CGContextAddLineToPoint(context, 80,80);
	CGContextStrokePath(context);
}
@end

@interface ArcView : BaseShape {
}
@end
@implementation ArcView
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetStrokeColor(context, colorComponents);
	CGContextMoveToPoint(context, 5, 5);
	CGContextAddArcToPoint(context,95,5,95,95,80);	
	CGContextDrawPath(context, kCGPathStroke);
}
@end

@interface CurvView : BaseShape {
}
@end
@implementation CurvView
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetStrokeColor(context, colorComponents);
	CGContextMoveToPoint(context, 5, 50);
	CGContextAddCurveToPoint(context,25,25,75,75,95,50);	
	CGContextDrawPath(context, kCGPathStroke);
}
@end

@interface EllipseView : BaseShape {
}
@end
@implementation EllipseView
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetStrokeColor(context, colorComponents);
	CGContextAddEllipseInRect(context, CGRectMake(5,30,90,50));
	CGContextDrawPath(context, kCGPathStroke);
}
@end

@interface PathView : BaseShape {
	CGMutablePathRef path;
}
@end
@implementation PathView
- (id)initWithFrame:(CGRect)frame
{
	if (self = [super initWithFrame:frame]) {
		path = CGPathCreateMutable();
		CGPathMoveToPoint(path,NULL,5,5);
		CGPathAddLineToPoint(path,NULL,20,20);
		CGPathAddCurveToPoint(path,NULL,0,50,100,70,95,95);
		//CGPathCloseSubpath(path);
	}
	return self;
}
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetStrokeColor(context, colorComponents);
	CGContextAddPath(context, path);
	CGContextStrokePath(context);
}
-(void)dealloc
{
	[super dealloc];
	CFRelease(path);
}
@end

@interface FillView : BaseShape {
}
@end
@implementation FillView
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawFrame:context];
	
	CGFloat colorComponents[] = {1.0,0.0,0.0,1.0};
	CGContextSetFillColor(context, colorComponents);
	CGMutablePathRef path = CGPathCreateMutable();
	CGPathAddEllipseInRect(path,NULL, CGRectMake(20,20,60,60));
	CGPathAddEllipseInRect(path,NULL, CGRectMake(30,30,40,40));
	CGPathCloseSubpath(path);
	CGContextAddPath(context, path);
	CGContextEOFillPath(context);
	CFRelease(path);
}
@end

@implementation LineShape

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		SimpleLineView *line = [[SimpleLineView alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
		[self addSubview:line];
		[line release];
		
		ArcView *arc = [[ArcView alloc] initWithFrame:CGRectMake(100, 0, 100,100)];
		[self addSubview:arc];
		[arc release];
		
		CurvView *cur = [[CurvView alloc] initWithFrame:CGRectMake(200, 0, 100,100)];
		[self addSubview:cur];
		[cur release];
		
		EllipseView *ellipse = [[EllipseView alloc] initWithFrame:CGRectMake(0, 100, 100,100)];
		[self addSubview:ellipse];
		[ellipse release];
		
		PathView *path = [[PathView alloc] initWithFrame:CGRectMake(100, 100, 100,100)];
		[self addSubview:path];
		[path release];
		
		FillView *fill = [[FillView alloc] initWithFrame:CGRectMake(200, 100, 100,100)];
		[self addSubview:fill];
		[fill release];
    }
	
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
}



@end
